#include <stdio.h>
#include <stdlib.h>
#include <time.h> /* For time(NULL) */
#include <unistd.h> /* For getpid() */
#include <string.h> /* For initializatioj of names */

#include "contexts.h"

#define NOJI 6
#define NOMI 6
#define NOCONTEXTS 2

static int 
getwhich(const char *prompt,int maxchoice);

static int 
getchoice(char *args[]);

static void 
operations(Context *s);

static void 
tests(Context *s);

static void
print_state(void);

static
Context s[NOCONTEXTS];
char *snames[NOCONTEXTS];

int 
main(void)
{
  int i;
  
  /* Initializations */
  srand(time(NULL) * getpid());

  {
    char *prefix="Context";
    char postfix[15]; /* 15 is enough ... */
    /* unless we have a very big number of subsets ... */
    /* which is not the case */
    
    for(i=0;i<NOCONTEXTS;i++)
      {
	snprintf(postfix,15,"%d",i);
	snames[i] = malloc(strlen(prefix) + strlen(postfix) + 2);
	strcpy(snames[i],prefix);
	strcat(snames[i],postfix);
      }
  }

  for(i=0;i<NOCONTEXTS;i++)
      ctx_init(NOJI,NOMI,s + i);

  system("clear");
  print_state();
  printf("\n");
      
  for(;;)
    {

      char *menu[]={
	"Exit",
	"Operations on contexts",
	"Tests on contexts",
	NULL
      };
      
      
      printf("Main Menu\n");
      switch(getchoice(menu))
	{
	case 0:
	   system("clear");
	  exit(EXIT_SUCCESS);
	case 1:
	  operations(s);
	  break;
	case 2:
	  tests(s);
	  break;
	default:
	  continue;
	}

      system("clear");
      print_state();
      printf("\n");
      
    }

  exit(EXIT_FAILURE);
}



void
operations(Context *sptr)
{

  system("clear");
  print_state();
  printf("\n");
  
  for(;;)
    {
      char *args[]={
	"Done",
	"Turn a context into its complement",
	"Set a context at random",
	"Copy a context",
	"Parse a context from stdin",
	NULL
      };

      printf("Menu Operations on Contexts\n");      
      switch(getchoice(args))
	{
	case 0:
	  return;
	case 1:
	  ctx_complement(
			sptr + getwhich("Which context",(NOCONTEXTS-1))
			);
	  break;
	case 2:
	  ctx_rand(
		   sptr+ getwhich("Which context",(NOCONTEXTS-1))
		   );
	  break;
	case 3:
	  { 
	    int first,dest;
	    
	    first=getwhich("Context to copy",(NOCONTEXTS-1));
	    do
	      {
		printf("Destination context"
		       " (must be different from previous)");
		dest=getwhich("",(NOCONTEXTS-1));
	      }
	    while(dest == first);
	    ctx_cpy(sptr + dest, sptr + first);
	  }	 
	  break;
	case 4:
	   { 
	    int dest;
	    
	    dest=getwhich("Destination context",(NOCONTEXTS-1));
	    ctx_del_names(sptr + dest);
	    ctx_init(0,0,sptr + dest);
	    ctx_parse(sptr + dest);
	  }
	  break;
	default:
	  continue;
	}
      
      system("clear");
      print_state();
      printf("\n");
      
   }

}


void
tests(Context *sptr)
{
  int ret = 0;
  
  system("clear");
  print_state();
  printf("\n");

  for(;;)
    {
      char *menu[]={
	"Done",
	NULL
      };

      printf("Menu Tests on Contexts\n");
      switch(getchoice(menu))
	{
	case 0:
	  return;
	default:
	  continue;
	}

      if(ret)
	printf("Answer is yes\n");
      else
	printf("Answer is no\n");
      
      sleep(3);
      system("clear");
      print_state();
      printf("\n");
 
   }

}


void
print_state(void)
{
  int i;

  for(i=0;i<NOCONTEXTS;i++)
    {
      printf("%s :\n",snames[i]);
      ctx_print_matrix(s+i);
    }
}


int getwhich(const char *prompt,int maxchoice)
{
  int ret;

  printf("%s (0-%d) : ",prompt,maxchoice);
  fflush(stdout);

  ret = -1;
  while( ret <0 || ret > maxchoice )
    {
      scanf("%d",&ret);
      getchar();
    }

  return ret;

}

int getchoice(char *args[])
{
  int i = 0;
    
  while(args[i] != NULL)
    {
      printf("(%d) %s\n",i,args[i]);
      i++;
    }
 
  return getwhich("Enter your choice",i-1);

}
